<?php
if (!(defined('IN_IA'))) 
{
	exit('Access Denied');
}

//服务类门店
class Index_MxShopPage extends MobileLoginPage
{

    private $mendianTable = '';
    private $mendianCateTable = '';
    private $industryTable = '';
    private $pagesize = 10;

    public function __construct()
    {
        parent::__construct();

        $this->mendianTable = tablename('mx_shop_mendan');
        $this->mendianCateTable = tablename('mx_shop_mendan_cate');
        $this->industryTable = tablename('mx_shop_mendian_industry');
    }

    //门店列表
	public function main()
    {
		global $_W, $_GPC;

        //获取令牌
        $token = $this->getToken();

		include $this->template();
	}

    //根据省市获取门店列表接口
    public function getStores()
    {
        global $_W, $_GPC;

        //验证令牌
        if(empty($_GPC['token']) || !$this->checkToken($_GPC['token']))
        {
            show_json(0, '参数有误');
        }

        $page = empty($_GPC['page']) ? 1 : intval($_GPC['page']);
        $pagesize = $this->pagesize;

        $condition = ' d.uniacid=:uniacid ';
        $params = array(':uniacid'=>$_W['uniacid']);

        $province = $_GPC['province'];
        $city = $_GPC['city'];
        if(empty($province) && empty($city))
        {
            $province = '广东省';
            $city = '广州市';
        }

        $condition .= ' AND d.province=:province AND d.city=:city ';
        $params[':province'] = $province;
        $params[':city'] = $city;

        $condition .= ' AND d.status=1 AND d.deleted=0 ';

        $sql = 'SELECT d.*, c.name as cate, i.name as industry FROM '.$this->mendianTable.' d ';
        $join = ' LEFT JOIN '.$this->mendianCateTable.' c ON c.id=d.mdcate_id ';
        $join .= ' LEFT JOIN '.$this->industryTable.' i ON i.id=d.industryid ';
        $where = ' WHERE '. $condition;
        $orderby = ' ORDER BY id DESC';
        $limit = ' LIMIT '.($page-1) * $pagesize.','.$pagesize;

        $list = pdo_fetchall($sql.$join.$where.$orderby.$limit, $params);

        $sql = 'SELECT ifnull(count(*), 0) FROM '.$this->mendianTable.' d ';
        $total = pdo_fetchcolumn($sql.$where, $params);

        $res = array();
        if(!empty($list) && is_array($list))
        {
            foreach ($list as $index=>$item)
            {
                $res[$index]['title'] = $item['mendianname'];
                $res[$index]['number'] = $item['mdnumber'];
                $res[$index]['logo'] = $item['thumb'];

                $res[$index]['cate'] = $item['cate'];
                $res[$index]['industry'] = $item['industry'];

                $res[$index]['address'] = $item['address'];
                $res[$index]['province'] = $item['province'];
                $res[$index]['city'] = $item['city'];
                $res[$index]['area'] = $item['area'];

                $res[$index]['tel'] = $item['tel'];
                $res[$index]['lng'] = $item['lng'];
                $res[$index]['lat'] = $item['lat'];

                $res[$index]['date'] = date('Y-m-d H:i:s', $item['createtime']);
            }
        }

        show_json(1, array('total'=>$total, 'pagesize'=>$pagesize, 'data'=>$res));
    }

    //获取已有门店所有省市接口
    public function getStoresCity()
    {
        global $_W, $_GPC;

        //验证令牌
        if (empty($_GPC['token']) || !$this->checkToken($_GPC['token']))
        {
            show_json(0, '参数有误');
        }

        $sql = 'SELECT ifnull(count(*), 0) as storenum, province FROM '.$this->mendianTable;
        $where = ' WHERE uniacid=:uniacid AND status=1 AND deleted=0';
        $group = ' GROUP BY province ';
        $params = array(':uniacid'=>$_W['uniacid']);

        $list = pdo_fetchall($sql.$where.$group, $params);

        $res = array();
        if(!empty($list) && is_array($list))
        {

            $sql = 'SELECT ifnull(count(*), 0) as storenum, city FROM '.$this->mendianTable;
            $where .= ' AND city!=""';
            $group = ' GROUP BY city ';

            foreach ($list as $index=>$item)
            {
                if(!empty($item['province']))
                {
                    $res[$index]['province'] = $item['province'];
                    $res[$index]['storenum'] = $item['storenum'];
                    $res[$index]['city'] = pdo_fetchall($sql.$where.$group, $params);

                }
            }
        }

        show_json(1, array('data'=>$res));
    }
    
    /*
     * 统计门店数(不传参数返回所有门店数)
     * @param string $province 省（可为空）
     * @param string $city 市（可为空）
     * @return int 门店数
     * */
    protected function countStores($province='', $city='')
    {

        global $_W,$_GPC;
        
        $condition = ' uniacid=:uniacid ';
        $params = array(':uniacid'=>$_W['uniacid']);
        
        if(!empty($province))
        {
            $condition .= ' AND province=:province ';
            $params[':province'] = $province;
            if(!empty($city))
            {
                $condition .= ' AND city=:city ';
                $params[':city'] = $city;
            }
        }
        
        $condition .= ' AND status=1 AND deleted=0 ';
        
        $sql = 'SELECT ifnull(count(*), 0) FROM '.$this->mendianTable;
        $where = ' WHERE '. $condition;
        
        return pdo_fetchcolumn($sql.$where, $params);
    }

    //门店地图
    public function map()
    {
        global $_W, $_GPC;

        //获取令牌
        $token = $this->getToken();

        include $this->template();
    }

    //根据门店编号获取门店详情接口
    public function getStoreInfo()
    {
        global $_W, $_GPC;

        //验证令牌
        if(empty($_GPC['token']) || !$this->checkToken($_GPC['token']))
        {
            show_json(0, '参数有误');
        }

        $number = $_GPC['number'];
        if(empty($number)){
            show_json(0, '参数有误');
        }

        $store = $this->getDetail($number);

        $imgs = array();
        if(!empty($store['tuce']))
        {
            $tuce = unserialize($store['tuce']);
            foreach ($tuce as $item)
            {
                $imgs[] = tomedia($item);
            }
            unset($tuce);
        }

        $res = array
        (
            'number' => $store['mdnumber'],
            'title' => $store['mendianname'],
            'cate' => $store['cate'],
            'industry' => $store['industry'],
            'logo' => $store['thumb'],
            'imgs' => $imgs,
            'province' => $store['province'],
            'city' => $store['city'],
            'area' => $store['area'],
            'tel' => $store['tel'],
            'lng' => $store['lng'],
            'lat' => $store['lat'],
            'address' => $store['address'],
            'desc' => $store['description'],
        );


        show_json(1, array('data'=>$res));
    }

    /*
     * 根据门店编号查询门店详情
     * @param string $number 门店编号
     * @return array/null;
     */
    protected function getDetail($number)
    {
        global $_W;

        if(empty($number))
        {
            return null;
        }

        $sql = 'SELECT d.*, c.name as cate, i.name as industry FROM '.$this->mendianTable.' d ';
        $join = ' LEFT JOIN '.$this->mendianCateTable.' c ON c.id=d.mdcate_id ';
        $join .= ' LEFT JOIN '.$this->industryTable.' i ON i.id=d.industryid ';
        $where = ' WHERE d.mdnumber=:mdnumber AND d.uniacid=:uniacid AND d.status=1 AND d.deleted=0';
        $limit = ' LIMIT 1';
        $params = array(':mdnumber'=>$number, ':uniacid'=>$_W['uniacid']);

        return pdo_fetch($sql.$join.$where.$limit, $params);

    }

}
